<?php 


namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Cookie;


class news extends Controller
{

    /**
     * [dynamic 加载首页]
     */
    public function dynamic()
    {
        $news = db('_news_dynamic')
            ->where('status='.'1')
            ->select();
        //var_dump($news);die;
        return view('index@news/dynamic',[
            'news' => $news,
            'loginname' => Cookie::get('loginname')
        ]);
    }

    /**
     * [detail 加载编辑新闻页面]
     */
    public function detail()
    {
        $res = input('param.');
        
        //var_dump($res);die;
        $id = $res['id'];
        $res = db('_news_dynamic')->field(['id','content','name','title','image','status'])->find($id);
        //var_dump($res);die;
        return view('index@news/detail',[
            'res' => $res,
            'loginname' => Cookie::get('loginname')
        ]);

    }


    /**
     * [search 搜索新闻]
     */
    public function search()
    {
        $res = input('param.');
        $search = $res['search'];
        //var_dump($search);die;
        $curl = curl_init();
        $apikey = '952197d16d7be820ac5c579d5a2f9441';
        $word = urlencode($search);
        $url = 'http://api.tianapi.com/social/?key='.$apikey.'&num=10&word='.$word;
        //var_dump($word);die;
        // CURL设置
        curl_setopt($curl, CURLOPT_URL, $url);
        // 将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        // CURL  执行
        $data = curl_exec($curl);

        // 关闭CURL 
        curl_close($curl);

        $jsonObj = json_decode($data);
        //var_dump($jsonObj);
        if ($jsonObj->code !=200) {
            return $this->error('查无数据');
        }
        $newslist = $jsonObj->newslist;
        $new['search'] = $search;
        //var_dump($newslist);die;
        return view('index@news/search',[
            'newslist' => $newslist,
            'new' => $new,
            'loginname' => Cookie::get('loginname')
        ]);


    }

    public function _empty(Request $request)
    {
        return view('/error');
    }




}











